package avicit.cbb.techclass.tabledefine.cbbtabcolumnstl.controller.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import avicit.cbb.techclass.tabledefine.cbbtabcolumnstl.dao.CbbTabColumnsTlDao;
import avicit.cbb.techclass.tabledefine.cbbtabcolumnstl.dto.CbbTabColumnsTlDTO;
import avicit.platform6.api.syslanguage.SysLanguageAPI;
import avicit.platform6.api.syslanguage.dto.SysLanguage;
import avicit.platform6.commons.utils.ComUtil;
import avicit.platform6.commons.utils.PojoUtil;
import avicit.platform6.core.properties.PlatformConstant.OpType;
import avicit.platform6.core.domain.BeanBase;
import avicit.platform6.core.exception.DaoException;
import avicit.platform6.core.service.ServiceBase;
import avicit.platform6.core.sfn.intercept.SelfDefinedQuery;
import avicit.platform6.core.jdbc.JdbcAvicit;
import avicit.platform6.core.mybatis.pagehelper.Page;
import avicit.platform6.core.mybatis.pagehelper.PageHelper;
import avicit.platform6.core.rest.msg.QueryReqBean;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.modules.system.syslog.service.SysLogUtil;

/**
 * @classname:  CbbTabColumnsTlService
 * @description: 定义 库表结构定义多语言表实现类
 * @author:  AVICIT DEV
 */
@Service
public class CbbTabColumnsTlService extends ServiceBase implements Serializable {

	private static final Logger logger =  LoggerFactory.getLogger(CbbTabColumnsTlService.class);
	
    private static final long serialVersionUID = 1L;
    
//	@Autowired
//	private SysLogUtil sysLogUtil;
	
	@Autowired
	private CbbTabColumnsTlDao dao;
	@Autowired
	private SysLanguageAPI sysLanguageAPI;
	@Autowired
	private JdbcAvicit jdbcAvicit;
	/**
	 * 按条件分页查询
	 * @param queryReqBean
	 * @return
	 * @throws Exception
	 */
	public QueryRespBean<CbbTabColumnsTlDTO> searchCbbTabColumnsTlByPage(QueryReqBean<CbbTabColumnsTlDTO> queryReqBean,String sfnConditions) throws Exception {
		try{
			PageHelper.startPage(queryReqBean.getPageParameter());
			CbbTabColumnsTlDTO searchParams = queryReqBean.getSearchParams();
            SelfDefinedQuery sdc=new SelfDefinedQuery(sfnConditions,"t1");
			Page<CbbTabColumnsTlDTO> dataList =  dao.searchCbbTabColumnsTlByPage(searchParams,sdc.parseSql());
			QueryRespBean<CbbTabColumnsTlDTO> queryRespBean =new QueryRespBean<CbbTabColumnsTlDTO>();

			queryRespBean.setResult(dataList);
			return queryRespBean;
		}catch(DaoException e){
			logger.error("searchCbbTabColumnsTlByPaging出错：", e);
			throw e;
		}
	}
	/**
	 * 按条件查询，不分页
	 * @param queryReqBean
	 * @return
	 * @throws Exception
	 */
	public List<CbbTabColumnsTlDTO> searchCbbTabColumnsTl(QueryReqBean<CbbTabColumnsTlDTO> queryReqBean) throws Exception {
	    try{
	    	CbbTabColumnsTlDTO searchParams = queryReqBean.getSearchParams();
	    	List<CbbTabColumnsTlDTO> dataList = dao.searchCbbTabColumnsTl(searchParams);
	    	return dataList;
	    }catch(Exception e){
	    	logger.error("searchCbbTabColumnsTl出错：", e);
	    	throw new DaoException("",e);
	    }
    }
	/**
	 * 通过主键查询单条记录
	 * @param id
	 * @param logBase
	 * @return
	 * @throws Exception
	 */
	public CbbTabColumnsTlDTO queryCbbTabColumnsTlByPrimaryKey(String id) throws Exception {
		try{
			CbbTabColumnsTlDTO dto = dao.findCbbTabColumnsTlById(id);
			//记录日志
			SysLogUtil.log4Query(dto);
			return dto;
		}catch(Exception e){
	    	logger.error("searchCbbTabColumnsTl出错：", e);
	    	throw new DaoException("",e);
	    }
	}

	/**
	 * 新增对象
	 * @param dto
	 * @param logBase
	 * @return
	 * @throws Exception
	 */
	public String insertCbbTabColumnsTl(CbbTabColumnsTlDTO dto) throws Exception {
		try{
			String id = ComUtil.getId();
			dto.setId(id);
			PojoUtil.setSysProperties(dto, OpType.insert);
			dao.insertCbbTabColumnsTl(dto);
			//记录日志
			SysLogUtil.log4Insert(dto);
			return id;
		}catch(Exception e){
			logger.error("insertCbbTabColumnsTl出错：", e);
			throw new DaoException("",e);
		}
	}
	/**
	 * 修改对象全部字段
	 * @param dto
	 * @param logBase
	 * @return
	 * @throws Exception
	 */
	public int updateCbbTabColumnsTl(CbbTabColumnsTlDTO dto) throws Exception {
			//记录日志
			CbbTabColumnsTlDTO old =findById(dto.getId());
			SysLogUtil.log4Update(dto, old);
			PojoUtil.setSysProperties(dto, OpType.update);
			PojoUtil.copyProperties(old, dto,true);
			int ret = dao.updateCbbTabColumnsTlAll(old);
			if(ret ==0){
				throw new DaoException("数据失效，请从新更新");
			}
			return ret;

	}
	/**
	 * 修改对象部分字段
	 * @param dto
	 * @param logBase
	 * @return
	 * @throws Exception
	 */
	public int updateCbbTabColumnsTlSensitive(CbbTabColumnsTlDTO dto) throws Exception {
		try{
			//记录日志
			CbbTabColumnsTlDTO old =findById(dto.getId());
			SysLogUtil.log4Update(dto, old);
			PojoUtil.setSysProperties(dto, OpType.update);
			PojoUtil.copyProperties(old, dto,true);
			int count = dao.updateCbbTabColumnsTlSensitive(old);
			if(count ==0){
				throw new DaoException("数据失效，请从新更新");
			}
			return count;
		}catch(Exception e){
			logger.error("searchDemoBusinessTrip出错：", e);
			throw new DaoException("",e);
		}

	}
	
	/**
	 * 按主键单条删除
	 * @param id
	 * @param logBase
	 * @return
	 * @throws Exception
	 */
	public int deleteCbbTabColumnsTlById(String id) throws Exception {
		if(StringUtils.isEmpty(id)) throw new Exception("删除失败！传入的参数主键为null");
		try{
			//记录日志
			SysLogUtil.log4Delete(findById(id));
			return dao.deleteCbbTabColumnsTlById(id);
		}catch(Exception e){
			logger.error("deleteCbbTabColumnsTl出错：", e);
			throw new DaoException("",e);
		}
	}

	/**
	 * 批量删除数据
	 * @param ids id的数组
	 * @return
	 * @throws Exception
	 */
	public int deleteCbbTabColumnsTlByIds(String[] ids) throws Exception{
		int result =0;
		for(String id : ids ){
			deleteCbbTabColumnsTlById(id);
			result++;
		}
		return result;
	}
		
	/**
	 * 日志专用，内部方法，不再记录日志
	 */
	private CbbTabColumnsTlDTO findById(String id) throws Exception {
		try{
			CbbTabColumnsTlDTO dto = dao.findCbbTabColumnsTlById(id);
			return dto;
		}catch(DaoException e){
	    		logger.error("findCbbTabColumnsTl出错：", e);
	    		throw e;
	    }
	}
/**
 * 获取多语言信息		
 * @param id
 * @return
 */
	public List<Map<String,Object>> getAllLanguageById(String id) throws Exception {
	    List<SysLanguage> sysLanguageCollection = this.sysLanguageAPI.getAllSysLanguages();

	    List<Map<String,Object>> result = new ArrayList<Map<String,Object>>();

	     Map<String,Object> map=null;
	    for (SysLanguage language : sysLanguageCollection) {
	    	CbbTabColumnsTlDTO temp = getSysLookupTypeTlByLanguageCode(id, language.getLanguageCode());

	        map = new HashMap<String,Object>();
	        String tlid = "";
	        String columnsShowName = "";
	        String description = "";
	        if (temp != null) {
	          tlid = temp.getId();
	          columnsShowName = temp.getColumnsShowName();
	          description = temp.getDescription();
	        }

	      map.put("id", tlid);
	      map.put("columnsShowName", columnsShowName);
	      map.put("description", description);
	      map.put("sysLanguageCode", language.getLanguageCode());
	      map.put("sysLanguageName", language.getLanguageName());
	      map.put("cbbTabColumnsId", id);
	      
	      result.add(map);
	    }
	    return result;
	  }
	@SuppressWarnings({"static-access", "unchecked", "rawtypes"})
	public CbbTabColumnsTlDTO getSysLookupTypeTlByLanguageCode(String pId, String sysLanguageCode)
	  {
	    if (null == pId) return null;
	    Assert.notNull(sysLanguageCode);
	    String sql = "select  * from CBB_TAB_COLUMNS_TL t where t.CBB_TAB_COLUMNS_ID =:pid and t.sys_language_code =:language";
	    MapSqlParameterSource ss = new MapSqlParameterSource();
	    ss.addValue("pid", pId, 12);
	    ss.addValue("language", sysLanguageCode, 12);
	    try {
	      return (CbbTabColumnsTlDTO)this.jdbc.getNamedParameterJdbcOperations().queryForObject(sql, ss, new BeanPropertyRowMapper(CbbTabColumnsTlDTO.class));
	    } catch (EmptyResultDataAccessException e) {
	      this.logger.debug("没有查到数据！");
	    }return null;
	  }
	@Override
	public void delete(BeanBase arg0) throws DaoException {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void insert(BeanBase arg0) throws DaoException {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void update(BeanBase arg0) throws DaoException {
		// TODO Auto-generated method stub
		
	}
public List<Map<String, Object>> findSysLanguageByParentId(String id){
	String sql="select * from Cbb_Tab_Columns_tl where CBB_TAB_COLUMNS_ID='"+id+"'";
	List<Map<String, Object>> list=jdbcAvicit.getJdbcTemplate().queryForList(sql);
	return list;
}
public List<Map<String, Object>> findSysLanguageByParentId2(String id){
	String sql="select * from CBB_TEMPLET_FIELD_TL where CBB_TEMPLET_FIELD_ID='"+id+"'";
	List<Map<String, Object>> list=jdbcAvicit.getJdbcTemplate().queryForList(sql);
	return list;
}
}

